#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""PDF数据模型"""

from typing import List, Dict, Any, Optional
from pydantic import BaseModel, Field
from datetime import datetime

class PDFMetadata(BaseModel):
    """PDF文件元数据"""
    filename: str = Field(..., description="文件名")
    page_count: int = Field(..., description="页数")
    created_at: Optional[datetime] = Field(None, description="创建时间")
    modified_at: Optional[datetime] = Field(None, description="修改时间")
    author: Optional[str] = Field(None, description="作者")
    title: Optional[str] = Field(None, description="标题")
    subject: Optional[str] = Field(None, description="主题")
    keywords: Optional[List[str]] = Field(None, description="关键词")
    producer: Optional[str] = Field(None, description="生成器")
    creator: Optional[str] = Field(None, description="创建工具")

class TableCell(BaseModel):
    """表格单元格"""
    value: Any = Field(..., description="单元格值")
    row: int = Field(..., description="行索引")
    col: int = Field(..., description="列索引")
    rowspan: int = Field(1, description="行合并数")
    colspan: int = Field(1, description="列合并数")

class TableData(BaseModel):
    """表格数据"""
    id: int = Field(..., description="表格ID")
    title: Optional[str] = Field(None, description="表格标题")
    page: int = Field(..., description="所在页码")
    is_continued: bool = Field(False, description="是否是续表")
    continued_from: Optional[int] = Field(None, description="续表来源ID")
    rows: int = Field(..., description="行数")
    cols: int = Field(..., description="列数")
    headers: List[str] = Field([], description="表头")
    data: List[List[Any]] = Field(..., description="表格数据")
    raw_data: Optional[List[List[Any]]] = Field(None, description="原始表格数据")
    structured_data: Optional[Dict[str, Any]] = Field(None, description="结构化数据")

class ExtractResponse(BaseModel):
    """PDF提取响应"""
    filename: str = Field(..., description="文件名")
    metadata: PDFMetadata = Field(..., description="PDF元数据")
    tables: List[TableData] = Field(..., description="提取的表格数据")
    extracted_at: datetime = Field(default_factory=datetime.now, description="提取时间")

class TableRule(BaseModel):
    """表格匹配规则"""
    id: str = Field(..., description="规则ID")
    name: str = Field(..., description="规则名称")
    description: Optional[str] = Field(None, description="规则描述")
    title_pattern: Optional[str] = Field(None, description="标题匹配模式")
    header_pattern: Optional[List[str]] = Field(None, description="表头匹配模式")
    structure_pattern: Optional[Dict[str, Any]] = Field(None, description="结构匹配模式")
    content_pattern: Optional[Dict[str, Any]] = Field(None, description="内容匹配模式")
    priority: int = Field(0, description="规则优先级")
    enabled: bool = Field(True, description="是否启用")